const baseUrl = 'http://localhost:8080';
async function ajax(url, method, data) {
    url = baseUrl + url;
    if(!method){
        method = 'GET'
    }
    if(!data){
        data = {}
    }
    return new Promise(function (resolve, reject) {
        let ajaxSetting = {
            url: url,
            type: method,
            data:data,
            success: function (response) {
                resolve(response);
            },
            error: function () {
                reject("请求失败");
            }
        }
        $.ajax(ajaxSetting);
    });
}

const getLoginUser           = async (data) =>{return ajax('/api/user/loginUser','GET', data)}
// 根据文章ID查询文章
const getArticleById         = async (articleId) =>{return ajax('/api/article/'+articleId, 'GET', {})}
// 文章分页查询,{page,size,title,keywords}
const getArticles            = async (data) =>{return ajax('/api/article', 'GET', data)}
// 发送评论信息
const sendComment            = async (data) =>{return ajax('/api/comments', 'POST', data)}
// 添加评论回复{commentId, to, content}
const addReply               = async (data) =>{return ajax('/api/comments/addReply', 'POST', data)}
// 根据文章ID获取评论信息
const getCommentsByArticleId = async (data) =>{return ajax('/api/comments/getInfo', 'get', data)}
// 课程搜索{key,gradeId}
const getCourses             = async (data) =>{return ajax('/api/course', 'get', data)}
// 根据课程ID获取大纲信息{courseId}
const getOutlines            = async (data) =>{return ajax('/api/outline', 'get', data)}
// 根据大纲ID获取大纲
const getOutlineById         = async (outlineId) =>{return ajax('/api/outline/'+outlineId, 'get', {})}
// 上传文章
const saveArticle            = async (data) =>{return ajax('/api/article', 'POST', data)}
// 删除视频，不可用
const deleteVideo            = async (id) =>{return ajax('/api/video/'+id, 'delete', {});}
const deleteArticle          = async (id) =>{return ajax('/api/article/'+id, 'delete', {});}
// 获取文章、用户是否点赞
const getArticleZanState     = async (articleId) =>{return ajax('/api/article/zanState', 'get', {articleId})}
// 点赞{articleId,}
const articleZan             = async (data) =>{return ajax('/api/article/zan', 'POST', data)}
// 获取点赞个数
const articleZanCount        = async (articleId) =>{return ajax('/api/article/zanCount', 'get', {articleId})}
// 大纲demo
const outlineDemo            = async () =>{return ajax('/api/outline/demo', 'get', {})}
// 判断是否收藏文章
const articleCollectState    = async (articleId) =>{return ajax('/api/article/collectState', 'get', {articleId})}
// 收藏文章
const articleCollect         = async (articleId) =>{return ajax('/api/article/collect', 'POST', {articleId})}
// 获取用户的文章数量
const articleCountOfUser     = async (userId) =>{return ajax('/api/user/articleCountOfUser','get',{userId})}
// 用户关注状态
const followState            = async (userId) =>{return ajax('/api/follow/state','get',{userId})}
// 取消挂住
const cancelFollow           = async (userId) =>{return ajax('/api/follow','delete',{userId})}
// 取消挂住
const follow                 = async (userId) =>{return ajax('/api/follow','post',{userId})}
const getCity                = async (data) =>{return ajax('/api/city','get',data)}
const getSchools             = async (data) =>{return ajax('/api/school','get',data)}
// 根据area三级城市获取二级城市信息
const getCityByArea          = async (data) =>{return ajax('/api/city/getCityByArea','get',data)}
// 更新用户
const updateUser             = async (data) =>{return ajax('/api/user/update','put',data)}


// 视频点赞 {videoId}
const videoZan               = async (data) =>{return ajax('/api/video/zan', 'POST', data)}
// 获取视频zan状态
const getVideoZanState       = async (videoId) =>{return ajax('/api/video/zanState', 'get', {videoId})}
// 获取视频点赞个数
const videoZanCount          = async (videoId) =>{return ajax('/api/video/zanCount', 'get', {videoId})}
const videoPreview           = async () =>{return ajax('/api/video/preview', 'get', {})}
const saveVideo              = async (data) =>{return ajax('/api/video', 'post', data)}

// 视频评论 {videoId,user.id,content}
const videoComment           = async (data) =>{return ajax('/api/videoComment', 'post', data)}
const getCommentsByVideoId   = async (videoId) =>{return ajax('/api/videoComment/'+videoId, 'get', {})}
// {commentId,toId,toUsername,content}
const videoReply             = async (data) =>{return ajax('/api/videoComment/addReply', 'get', data)}

const api = {
    getLoginUser,
    getArticleById,            // articleId
    getArticles,               // {page,size,title,keywords}
    sendComment,               // 添加评论
    addReply,                  // 添加评论回复
    getCommentsByArticleId,    // 获取文章评论信息
    getCourses,                // {key,gradeId}
    getOutlines,               // {courseId}
    getOutlineById,            // 根据大纲ID获取大纲
    saveArticle,               // 添加文章
    deleteVideo,               // 删除视频
    deleteArticle,
    getArticleZanState,        // 文章点赞状态
    articleZan,
    outlineDemo,               // 大纲演示
    articleCollectState,       // 文章收藏状态
    articleZanCount,           // 获取文章点赞数量
    articleCollect,            // 收藏文章
    articleCountOfUser,        // 用户的文章数量
    followState,               // 用户关注状态
    cancelFollow,              // 取消关注
    follow,                    // 关注作者
    getCity,                   // 获取城市信息
    getSchools,                // 获取学校信息
    getCityByArea,             // 根据area三级城市获取二级城市信息
    updateUser,                // 更新用户
    videoZan,                  // 视频点赞 {videoId}
    getVideoZanState,
    videoZanCount,             // 获取视频点赞个数
    videoComment,              // 视频评论 {videoId,user.id,content}
    getCommentsByVideoId,      // 根据视频ID获取评论信息
    videoReply,                // {commentId,toId,toUsername,content}
    videoPreview,              // 视频预览
    saveVideo,                 // 视频预览
}
